package basic.chapter3;

/**
 * @author Kevin
 * @date 2020/2/20 17:19
 */
public class SafeCalc {
    long value = 0L;
    synchronized long get() {
        return value;
    }
    synchronized void addOne() {
        value += 1;
    }
    synchronized void add10k() {
        int idx = 0;
        while (idx++ < 10000) {
            value += 1;
        }
    }
    public static void main(String[] args) throws InterruptedException {
        SafeCalc s = new SafeCalc();
        Thread th1 = new Thread(() -> s.add10k());
        Thread th2 = new Thread(() -> s.add10k());
        th1.start();
        th2.start();
        th1.join();
        th2.join();
        System.out.println(s.get());
    }
}
